﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;
using Obsidian.Action;
using Obsidian.Web;
using Obsidian.Edm;
using Obsidian.Runtime;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Data;


namespace Oak.MagSys.Logic
{
    public class AdminMenuItemLG : OakLG<ISession>
    {

        public AdminMenuItemLG(ISession session) : base(session) { }

        public bool UpdateAdminMenuItemsByAdminId(long adminId, IEnumerable<long> menuItemIds, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前账号没有权限执行该操作";
                return false;
            }

            List<MsAdminMenuItemInfo> list = new List<MsAdminMenuItemInfo>();
            foreach (long menuItemId in menuItemIds)
            {
                MsAdminMenuItemInfo item = new MsAdminMenuItemInfo();
                item.AdminId.Set(adminId);
                item.MenuItemId.Set(menuItemId);
                item.Created.Now();
                list.Add(item);
            }

            AdminMenuItemDA itemDA = new AdminMenuItemDA();
            try
            {
                itemDA.UpdteAdminMenuItemsByAdminId(adminId, list);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return false;
            }

            msg = "";
            return true;

        }

        public List<MsAdminMenuItemInfo> GetAdminMenuItemList(MsAdminMenuItemQry qry, out string msg)
        {
            List<MsAdminMenuItemInfo> list;

            try
            {
                AdminMenuItemDA itemDA = new AdminMenuItemDA();
                list = itemDA.SelectList(qry);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return list;

        }

        public List<MsMenuItemInfo> ListingAdminMenuItems(out string msg)
        {
            if (!Session.IsAdmin)
            {
                msg = "当前账号没有权限执行该操作";
                return null;
            }

            long adminId = Session.AccountId;

            List<MsMenuItemInfo> list;
            try
            {
                if (Session.IsSuperAdmin)
                {
                    MenuItemDA menuItemDA = new MenuItemDA();
                    list = menuItemDA.SelectList(new MsMenuItemQry());
                }
                else
                {
                    AdminMenuItemDA itemDA = new AdminMenuItemDA();
                    MsAdminMenuItemQry qry = new MsAdminMenuItemQry();
                    qry.AdminId.Set(adminId);
                    qry.IsSystemMenuItem.Set(false);
                    List<MsAdminMenuItemInfo> items = itemDA.SelectList(qry);

                    list = new List<MsMenuItemInfo>(items.Count);
                    foreach (MsAdminMenuItemInfo item in items)
                        list.Add(item.MenuItem.Value);
                }
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return list;
        }
    }
}
